package edu.yctc.weblog.utils;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import edu.yctc.weblog.dao.CategoryDAO;
import edu.yctc.weblog.dto.BlogDetailDTO;
import edu.yctc.weblog.dto.BlogListDTO;
import edu.yctc.weblog.dto.BlogPageDTO;
import edu.yctc.weblog.entity.BlogDO;
import edu.yctc.weblog.entity.CategoryDO;

/**
 * 
 * @className DTOUtil
 * @description do和dto之间的防腐层
 * @author zyj
 * @date 2020年6月19日
 *
 */
@Component
public class DTOUtil {

    @Resource
    private CategoryDAO categoryDAO;

    public BlogListDTO coverBlogDO2BlogListDTO(BlogDO blogDO) {
        BlogListDTO blogListDTO = new BlogListDTO();
        String categoryName = categoryDAO.getCategoryDOById(blogDO.getCategoryId()).getName();
        String categoryIcon = categoryDAO.getCategoryDOById(blogDO.getCategoryId()).getIcon();
        blogListDTO.setBlogId(blogDO.getId());
        blogListDTO.setBlogTitle(blogDO.getTitle());
        blogListDTO.setBlogSubUrl(blogDO.getSubUrl());
        blogListDTO.setBlogCoverImage(blogDO.getCoverImage());
        blogListDTO.setBlogCategoryId(blogDO.getCategoryId());
        blogListDTO.setBlogCategoryIcon(categoryIcon);
        blogListDTO.setBlogCategoryName(categoryName);
        blogListDTO.setCreateTime(blogDO.getCreateTime());
        return blogListDTO;

    };

    public BlogDetailDTO coverBlogDO2BlogDetailDTO(BlogDO blogDO) {
        // commentCount和blogTags不设置
        CategoryDO categoryDO = categoryDAO.getCategoryDOById(blogDO.getCategoryId());
        BlogDetailDTO blogDetailDTO = new BlogDetailDTO();
        blogDetailDTO.setBlogId(blogDO.getId());
        blogDetailDTO.setBlogTitle(blogDO.getTitle());
        blogDetailDTO.setBlogCategoryId(blogDO.getCategoryId());
        blogDetailDTO.setBlogCategoryIcon(categoryDO.getIcon());
        blogDetailDTO.setBlogCategoryName(categoryDO.getName());
        blogDetailDTO.setBlogCoverImage(blogDO.getCoverImage());
        blogDetailDTO.setBlogViews(blogDO.getViews());
        blogDetailDTO.setEnableComment(blogDO.getIsEnableComment());
        blogDetailDTO.setBlogContent(blogDO.getContent());
        blogDetailDTO.setCreateTime(blogDO.getCreateTime());
        return blogDetailDTO;
    }

    public BlogPageDTO coverBlogDO2BlogPageDO(BlogDO blogDO) {
        BlogPageDTO blogPageDTO = new BlogPageDTO();
        String categoryName = categoryDAO.getCategoryDOById(blogDO.getCategoryId()).getName();
        blogPageDTO.setBlogId(blogDO.getId());
        blogPageDTO.setBlogTitle(blogDO.getTitle());
        blogPageDTO.setBlogCoverImage(blogDO.getCoverImage());
        blogPageDTO.setBlogViews(blogDO.getViews());
        blogPageDTO.setBlogStatus(blogDO.getStatus());
        blogPageDTO.setBlogCategoryName(categoryName);
        blogPageDTO.setCreateTime(blogDO.getCreateTime());
        return blogPageDTO;
    }

}
